<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal v-bind="$attrs" @register="registerModal" @ok="handleSubmit">
      <template #title>
        <span> {{ getTitle.value }} </span>
      </template>
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  //   import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    SportsAverageScore,
    sportsAverageScoreSave,
    sportsAverageScoreForm,
  } from '/@/api/sports/sportsAverageScore';
  const { showMessage } = useMessage();
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsAverageScore>({} as SportsAverageScore);
  const getTitle = computed(() => ({
    value: record.value.isNewRecord ? t('新增平均值') : t('编辑平均值'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('指标名称'),
      field: 'indexName',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: ({ values }) => {
        return !values.field7;
      },
    },
    {
      label: t('适配性别'),
      field: 'averageValueAdaptSex',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'sys_user_sex',
        allowClear: true,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('适配年级'),
      field: 'singleIndexAdaptGrade',
      component: 'Select',
      componentProps: {
        maxlength: 32,
        dictType: '	vke_grade',
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('适配行政级别'),
      field: 'adaptExecutiveLevel',
      component: 'RadioGroup',
      componentProps: {
        dictType: 's_index_area_level',
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('平均分'),
      field: 'averageScore',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });
  let averageValue;
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ loading: true });
    averageValue = data;
    let res = await sportsAverageScoreForm(data);
    record.value = (res.sportsAverageScore || {}) as SportsAverageScore;
    if (record.value.indexName == undefined) {
      record.value.indexName = data.indexName;
    }
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    // closeModal();
    try {
      setModalProps({ confirmLoading: true });
      const data = await validate();
      data.averageValueId = averageValue.averageValueId;
      data.indexId = averageValue.indexId;
      const res = await sportsAverageScoreSave(data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
